package com.lang.service;

import java.util.List;

import com.lang.beans.Parcel;
import com.lang.daoandimpl.ParcelDAO;
import com.lang.util.Config;

public class ParcelService {
	private ParcelDAO parceldao;

	private String size = null;
	private String type = null;
	private String weight = null;
	private String transType = null;
	private String status = null;
	private String paytype = null;
	private String comment = null;

	public ParcelDAO getParceldao() {
		return parceldao;
	}

	public void setParceldao(ParcelDAO parceldao) {
		this.parceldao = parceldao;
	}

	public boolean addPackage(Parcel parcel) {
		parceldao.save(parcel);
		return true;
	}

	/**
	 * 通过id获取包裹详细信息
	 * 
	 * @param id
	 * @return parcelInfo
	 * @author Lang
	 */
	public String getParcelInfoById(String id) {
		String parcelInfo = "";   //初始化包裹详细信息字符串
		int i = Integer.valueOf(id);
//		System.out.println("i=" + i);
		Parcel p = parceldao.findById(i);  //从数据哭获取包裹
		if (p != null) {    //判断是否为空
			changeString(p);   //进行整数和字符串的转换
			//组装成jon格式
			parcelInfo = "{id:\"" + p.getId() + "\",sendName:\""
					+ p.getSendName() + "\",sendTel:\"" + p.getSendTel()
					+ "\",sendAddr:\"" + p.getSendAddr() + "\",recName:\""
					+ p.getRecName() + "\",recTel:\"" + p.getRecTel()
					+ "\",recAddr:\"" + p.getRecAddr() + "\",size:\""
					+ this.size + "\",type:\"" + this.type + "\",weight:\""
					+ p.getWeight() + " 公斤\",transType:\"" + this.transType
					+ "\",status:\"" + this.status + "\",payType:\""
					+ this.paytype + "\",comment:\"" + this.comment + "\"}";
		}
		return parcelInfo;   //返回 json格式字符串
	}
	/**
	 * 获取全部派件
	 * @return
	 */
	public List getAllSendParcels(){
		List parcels=parceldao.findByType(new Integer(1));
		return parcels;
	}
	/**
	 * 获取全部收件
	 * @return
	 */
	public List getAllReceiveParcels(){
		List parcels=parceldao.findByType(new Integer(1));
		return parcels;
	}
	/**
	 * 根据状态获取包裹
	 * @param status
	 * @return
	 */
	public List getParcelsByStatus(int status){
		List parcels=parceldao.findByStatus(status);
		return parcels;
	}
	
	/**
	 * 进行整数和字符串的转换
	 * @param p
	 * @author Lang
	 */
	public void changeString(Parcel p) {
		// 包装方式
		switch (p.getSize()) {
		case 0: // 超大
			size = Config.SIZE_BIGEST;
			break;
		case 1: // 大
			size = Config.SIZE_BIG;
			break;
		case 2: // 中
			size = Config.SIZE_MID;
			break;
		case 3: // 小
			size = Config.SIZE_SMALL;
			break;
		case 4: // 专用信封
			size = Config.SIZE_SPECIALLY;
			break;
		}
		// 包裹类型
		switch (p.getType()) {
		case 0: // 收件
			type = Config.TYPE_RECEIVE;
			break;
		case 1: // 派件
			type = Config.TYPE_SEND;
			break;
		}
		// 运输方式
		switch (p.getTransType()) {
		case 0: // 公路运输
			this.transType = Config.TRANSTYPE_HIGHWAY;
			break;
		case 1: // 航空运输
			this.transType = Config.TRANSTYPE_AVIATION;
		}
		// 包裹状态
		switch (p.getStatus()) {
		case 0: // 未处理
			this.status = Config.PAECEL_UNDEAL_STR;
			break;
		case 1: // 正在处理
			this.status = Config.PARCEL_DEALING_STR;
			break;
		case 2: // 成功
			this.status = Config.PARCEL_DEAL_SUCCESS_STR;
			break;
		case 3: // 异常
			this.status = Config.PARCEL_DEAL_ABNORMAL_STR;
		}
		// 付款方式
		switch (p.getPayType()) {
		case 0: // 邮客付款
			this.paytype = Config.PAYTYPE_SENDER;
			break;
		case 1: // 对方付款
			this.paytype = Config.PAYTYPE_RECEIVER;
		}
//        System.out.println(p.getComment());
		if(p.getComment()==null){
			this.comment="无";
		}else{
			this.comment=p.getComment();
		}
		
	}

}
